home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / RCS / blockframe.c,v < prev    next >
Encoding:
Text File  |  1992-07-01  |  17.1 KB  |  694 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.17.11.47.59;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Get info from stack frames;
  26.    convert between frames, blocks, functions and pc values.
  27.    Copyright 1986, 1987, 1988, 1989, 1991 Free Software Foundation, Inc.
  28.  
  29. This file is part of GDB.
  30.  
  31. This program is free software; you can redistribute it and/or modify
  32. it under the terms of the GNU General Public License as published by
  33. the Free Software Foundation; either version 2 of the License, or
  34. (at your option) any later version.
  35.  
  36. This program is distributed in the hope that it will be useful,
  37. but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  39. GNU General Public License for more details.
  40.  
  41. You should have received a copy of the GNU General Public License
  42. along with this program; if not, write to the Free Software
  43. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  44.  
  45. #include "defs.h"
  46. #include "symtab.h"
  47. #include "bfd.h"
  48. #include "symfile.h"
  49. #include "objfiles.h"
  50. #include "frame.h"
  51. #include "gdbcore.h"
  52. #include "value.h"        /* for read_register */
  53. #include "target.h"        /* for target_has_stack */
  54. #include "inferior.h"        /* for read_pc */
  55.  
  56. /* Is ADDR inside the startup file?  Note that if your machine
  57.    has a way to detect the bottom of the stack, there is no need
  58.    to call this function from FRAME_CHAIN_VALID; the reason for
  59.    doing so is that some machines have no way of detecting bottom
  60.    of stack.  */
  61.  
  62. int
  63. inside_entry_file (addr)
  64.      CORE_ADDR addr;
  65. {
  66.   if (symfile_objfile) 
  67.   return (addr >= symfile_objfile -> ei.entry_file_lowpc &&
  68.       addr <  symfile_objfile -> ei.entry_file_highpc);
  69.   else return 0;
  70. }
  71.  
  72. /* Test a specified PC value to see if it is in the range of addresses
  73.    that correspond to the main() function.  See comments above for why
  74.    we might want to do this.
  75.  
  76.    Typically called from FRAME_CHAIN_VALID. */
  77.  
  78. int
  79. inside_main_func (pc)
  80. CORE_ADDR pc;
  81. {
  82.   if (symfile_objfile)
  83.   return (symfile_objfile -> ei.main_func_lowpc  <= pc &&
  84.       symfile_objfile -> ei.main_func_highpc > pc);
  85.   else return 0;
  86. }
  87.  
  88. /* Test a specified PC value to see if it is in the range of addresses
  89.    that correspond to the process entry point function.  See comments
  90.    in objfiles.h for why we might want to do this.
  91.  
  92.    Typically called from FRAME_CHAIN_VALID. */
  93.  
  94. int
  95. inside_entry_func (pc)
  96. CORE_ADDR pc;
  97. {
  98.   if (symfile_objfile)
  99.   return (symfile_objfile -> ei.entry_func_lowpc  <= pc &&
  100.       symfile_objfile -> ei.entry_func_highpc > pc);
  101.   else return 0;
  102. }
  103.  
  104. /* Address of innermost stack frame (contents of FP register) */
  105.  
  106. static FRAME current_frame;
  107.  
  108. /*
  109.  * Cache for frame addresses already read by gdb.  Valid only while
  110.  * inferior is stopped.  Control variables for the frame cache should
  111.  * be local to this module.
  112.  */
  113. struct obstack frame_cache_obstack;
  114.  
  115. /* Return the innermost (currently executing) stack frame.  */
  116.  
  117. FRAME
  118. get_current_frame ()
  119. {
  120.   /* We assume its address is kept in a general register;
  121.      param.h says which register.  */
  122.  
  123.   return current_frame;
  124. }
  125.  
  126. void
  127. set_current_frame (frame)
  128.      FRAME frame;
  129. {
  130.   current_frame = frame;
  131. }
  132.  
  133. FRAME
  134. create_new_frame (addr, pc)
  135.      FRAME_ADDR addr;
  136.      CORE_ADDR pc;
  137. {
  138.   struct frame_info *fci;    /* Same type as FRAME */
  139.  
  140.   fci = (struct frame_info *)
  141.     obstack_alloc (&frame_cache_obstack,
  142.            sizeof (struct frame_info));
  143.  
  144.   /* Arbitrary frame */
  145.   fci->next = (struct frame_info *) 0;
  146.   fci->prev = (struct frame_info *) 0;
  147.   fci->frame = addr;
  148.   fci->next_frame = 0;        /* Since arbitrary */
  149.   fci->pc = pc;
  150.  
  151. #ifdef INIT_EXTRA_FRAME_INFO
  152.   INIT_EXTRA_FRAME_INFO (0, fci);
  153. #endif
  154.  
  155.   return fci;
  156. }
  157.  
  158. /* Return the frame that called FRAME.
  159.    If FRAME is the original frame (it has no caller), return 0.  */
  160.  
  161. FRAME
  162. get_prev_frame (frame)
  163.      FRAME frame;
  164. {
  165.   /* We're allowed to know that FRAME and "struct frame_info *" are
  166.      the same */
  167.   return get_prev_frame_info (frame);
  168. }
  169.  
  170. /* Return the frame that FRAME calls (0 if FRAME is the innermost
  171.    frame).  */
  172.  
  173. FRAME
  174. get_next_frame (frame)
  175.      FRAME frame;
  176. {
  177.   /* We're allowed to know that FRAME and "struct frame_info *" are
  178.      the same */
  179.   return frame->next;
  180. }
  181.  
  182. /*
  183.  * Flush the entire frame cache.
  184.  */
  185. void
  186. flush_cached_frames ()
  187. {
  188.   /* Since we can't really be sure what the first object allocated was */
  189.   obstack_free (&frame_cache_obstack, 0);
  190.   obstack_init (&frame_cache_obstack);
  191.  
  192.   current_frame = (struct frame_info *) 0; /* Invalidate cache */
  193. }
  194.  
  195. /* Flush the frame cache, and start a new one if necessary.  */
  196. void
  197. reinit_frame_cache ()
  198. {
  199.   FRAME fr = current_frame;
  200.   flush_cached_frames ();
  201.   if (fr)
  202.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  203.                       read_pc ()));
  204. }
  205.  
  206. /* Return a structure containing various interesting information
  207.    about a specified stack frame.  */
  208. /* How do I justify including this function?  Well, the FRAME
  209.    identifier format has gone through several changes recently, and
  210.    it's not completely inconceivable that it could happen again.  If
  211.    it does, have this routine around will help */
  212.  
  213. struct frame_info *
  214. get_frame_info (frame)
  215.      FRAME frame;
  216. {
  217.   return frame;
  218. }
  219.  
  220. /* If a machine allows frameless functions, it should define a macro
  221.    FRAMELESS_FUNCTION_INVOCATION(FI, FRAMELESS) in param.h.  FI is the struct
  222.    frame_info for the frame, and FRAMELESS should be set to nonzero
  223.    if it represents a frameless function invocation.  */
  224.  
  225. /* Return nonzero if the function for this frame has a prologue.  Many
  226.    machines can define FRAMELESS_FUNCTION_INVOCATION to just call this
  227.    function.  */
  228.  
  229. int
  230. frameless_look_for_prologue (frame)
  231.      FRAME frame;
  232. {
  233.   CORE_ADDR func_start, after_prologue;
  234.   func_start = (get_pc_function_start (frame->pc) +
  235.         FUNCTION_START_OFFSET);
  236.   if (func_start)
  237.     {
  238.       after_prologue = func_start;
  239. #ifdef SKIP_PROLOGUE_FRAMELESS_P
  240.       /* This is faster, since only care whether there *is* a prologue,
  241.      not how long it is.  */
  242.       SKIP_PROLOGUE_FRAMELESS_P (after_prologue);
  243. #else
  244.       SKIP_PROLOGUE (after_prologue);
  245. #endif
  246.       return after_prologue == func_start;
  247.     }
  248.   else
  249.     /* If we can't find the start of the function, we don't really
  250.        know whether the function is frameless, but we should be able
  251.        to get a reasonable (i.e. best we can do under the
  252.        circumstances) backtrace by saying that it isn't.  */
  253.     return 0;
  254. }
  255.  
  256. /* Default a few macros that people seldom redefine.  */
  257.  
  258. #if !defined (INIT_FRAME_PC)
  259. #define INIT_FRAME_PC(fromleaf, prev) \
  260.   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
  261.           prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
  262. #endif
  263.  
  264. #ifndef FRAME_CHAIN_COMBINE
  265. #define    FRAME_CHAIN_COMBINE(chain, thisframe) (chain)
  266. #endif
  267.  
  268. /* Return a structure containing various interesting information
  269.    about the frame that called NEXT_FRAME.  Returns NULL
  270.    if there is no such frame.  */
  271.  
  272. struct frame_info *
  273. get_prev_frame_info (next_frame)
  274.      FRAME next_frame;
  275. {
  276.   FRAME_ADDR address;
  277.   struct frame_info *prev;
  278.   int fromleaf = 0;
  279.  
  280.   /* If the requested entry is in the cache, return it.
  281.      Otherwise, figure out what the address should be for the entry
  282.      we're about to add to the cache. */
  283.  
  284.   if (!next_frame)
  285.     {
  286.       if (!current_frame)
  287.     {
  288.       error ("You haven't set up a process's stack to examine.");
  289.     }
  290.  
  291.       return current_frame;
  292.     }
  293.  
  294.   /* If we have the prev one, return it */
  295.   if (next_frame->prev)
  296.     return next_frame->prev;
  297.  
  298.   /* On some machines it is possible to call a function without
  299.      setting up a stack frame for it.  On these machines, we
  300.      define this macro to take two args; a frameinfo pointer
  301.      identifying a frame and a variable to set or clear if it is
  302.      or isn't leafless.  */
  303. #ifdef FRAMELESS_FUNCTION_INVOCATION
  304.   /* Still don't want to worry about this except on the innermost
  305.      frame.  This macro will set FROMLEAF if NEXT_FRAME is a
  306.      frameless function invocation.  */
  307.   if (!(next_frame->next))
  308.     {
  309.       FRAMELESS_FUNCTION_INVOCATION (next_frame, fromleaf);
  310.       if (fromleaf)
  311.     address = next_frame->frame;
  312.     }
  313. #endif
  314.  
  315.   if (!fromleaf)
  316.     {
  317.       /* Two macros defined in tm.h specify the machine-dependent
  318.      actions to be performed here.
  319.      First, get the frame's chain-pointer.
  320.      If that is zero, the frame is the outermost frame or a leaf
  321.      called by the outermost frame.  This means that if start
  322.      calls main without a frame, we'll return 0 (which is fine
  323.      anyway).
  324.  
  325.      Nope; there's a problem.  This also returns when the current
  326.      routine is a leaf of main.  This is unacceptable.  We move
  327.      this to after the ffi test; I'd rather have backtraces from
  328.      start go curfluy than have an abort called from main not show
  329.      main.  */
  330.       address = FRAME_CHAIN (next_frame);
  331.       if (!FRAME_CHAIN_VALID (address, next_frame))
  332.     return 0;
  333.       address = FRAME_CHAIN_COMBINE (address, next_frame);
  334.     }
  335.   if (address == 0)
  336.     return 0;
  337.  
  338.   prev = (struct frame_info *)
  339.     obstack_alloc (&frame_cache_obstack,
  340.            sizeof (struct frame_info));
  341.  
  342.   if (next_frame)
  343.     next_frame->prev = prev;
  344.   prev->next = next_frame;
  345.   prev->prev = (struct frame_info *) 0;
  346.   prev->frame = address;
  347.   prev->next_frame = prev->next ? prev->next->frame : 0;
  348.  
  349. #ifdef INIT_EXTRA_FRAME_INFO
  350.   INIT_EXTRA_FRAME_INFO(fromleaf, prev);
  351. #endif
  352.  
  353.   /* This entry is in the frame queue now, which is good since
  354.      FRAME_SAVED_PC may use that queue to figure out it's value
  355.      (see tm-sparc.h).  We want the pc saved in the inferior frame. */
  356.   INIT_FRAME_PC(fromleaf, prev);
  357.  
  358.   return prev;
  359. }
  360.  
  361. CORE_ADDR
  362. get_frame_pc (frame)
  363.      FRAME frame;
  364. {
  365.   struct frame_info *fi;
  366.   fi = get_frame_info (frame);
  367.   return fi->pc;
  368. }
  369.  
  370. #if defined (FRAME_FIND_SAVED_REGS)
  371. /* Find the addresses in which registers are saved in FRAME.  */
  372.  
  373. void
  374. get_frame_saved_regs (frame_info_addr, saved_regs_addr)
  375.      struct frame_info *frame_info_addr;
  376.      struct frame_saved_regs *saved_regs_addr;
  377. {
  378.   FRAME_FIND_SAVED_REGS (frame_info_addr, *saved_regs_addr);
  379. }
  380. #endif
  381.  
  382. /* Return the innermost lexical block in execution
  383.    in a specified stack frame.  The frame address is assumed valid.  */
  384.  
  385. struct block *
  386. get_frame_block (frame)
  387.      FRAME frame;
  388. {
  389.   struct frame_info *fi;
  390.   CORE_ADDR pc;
  391.  
  392.   fi = get_frame_info (frame);
  393.  
  394.   pc = fi->pc;
  395.   if (fi->next_frame != 0)
  396.     /* We are not in the innermost frame.  We need to subtract one to
  397.        get the correct block, in case the call instruction was the
  398.        last instruction of the block.  If there are any machines on
  399.        which the saved pc does not point to after the call insn, we
  400.        probably want to make fi->pc point after the call insn anyway.  */
  401.     --pc;
  402.   return block_for_pc (pc);
  403. }
  404.  
  405. struct block *
  406. get_current_block ()
  407. {
  408.   return block_for_pc (read_pc ());
  409. }
  410.  
  411. CORE_ADDR
  412. get_pc_function_start (pc)
  413.      CORE_ADDR pc;
  414. {
  415.   register struct block *bl;
  416.   register struct symbol *symbol;
  417.   register struct minimal_symbol *msymbol;
  418.   CORE_ADDR fstart;
  419.  
  420.   if ((bl = block_for_pc (pc)) != NULL &&
  421.       (symbol = block_function (bl)) != NULL)
  422.     {
  423.       bl = SYMBOL_BLOCK_VALUE (symbol);
  424.       fstart = BLOCK_START (bl);
  425.     }
  426.   else if ((msymbol = lookup_minimal_symbol_by_pc (pc)) != NULL)
  427.     {
  428.       fstart = msymbol -> address;
  429.     }
  430.   else
  431.     {
  432.       fstart = 0;
  433.     }
  434.   return (fstart);
  435. }
  436.  
  437. /* Return the symbol for the function executing in frame FRAME.  */
  438.  
  439. struct symbol *
  440. get_frame_function (frame)
  441.      FRAME frame;
  442. {
  443.   register struct block *bl = get_frame_block (frame);
  444.   if (bl == 0)
  445.     return 0;
  446.   return block_function (bl);
  447. }
  448.  
  449. /* Return the blockvector immediately containing the innermost lexical block
  450.    containing the specified pc value, or 0 if there is none.
  451.    PINDEX is a pointer to the index value of the block.  If PINDEX
  452.    is NULL, we don't pass this information back to the caller.  */
  453.  
  454. struct blockvector *
  455. blockvector_for_pc (pc, pindex)
  456.      register CORE_ADDR pc;
  457.      int *pindex;
  458. {
  459.   register struct block *b;
  460.   register int bot, top, half;
  461.   register struct symtab *s;
  462.   struct blockvector *bl;
  463.  
  464.   /* First search all symtabs for one whose file contains our pc */
  465.   s = find_pc_symtab (pc);
  466.   if (s == 0)
  467.     return 0;
  468.  
  469.   bl = BLOCKVECTOR (s);
  470.   b = BLOCKVECTOR_BLOCK (bl, 0);
  471.  
  472.   /* Then search that symtab for the smallest block that wins.  */
  473.   /* Use binary search to find the last block that starts before PC.  */
  474.  
  475.   bot = 0;
  476.   top = BLOCKVECTOR_NBLOCKS (bl);
  477.  
  478.   while (top - bot > 1)
  479.     {
  480.       half = (top - bot + 1) >> 1;
  481.       b = BLOCKVECTOR_BLOCK (bl, bot + half);
  482.       if (BLOCK_START (b) <= pc)
  483.     bot += half;
  484.       else
  485.     top = bot + half;
  486.     }
  487.  
  488.   /* Now search backward for a block that ends after PC.  */
  489.  
  490.   while (bot >= 0)
  491.     {
  492.       b = BLOCKVECTOR_BLOCK (bl, bot);
  493.       if (BLOCK_END (b) > pc)
  494.     {
  495.       if (pindex)
  496.         *pindex = bot;
  497.       return bl;
  498.     }
  499.       bot--;
  500.     }
  501.  
  502.   return 0;
  503. }
  504.  
  505. /* Return the innermost lexical block containing the specified pc value,
  506.    or 0 if there is none.  */
  507.  
  508. struct block *
  509. block_for_pc (pc)
  510.      register CORE_ADDR pc;
  511. {
  512.   register struct blockvector *bl;
  513.   int index;
  514.  
  515.   bl = blockvector_for_pc (pc, &index);
  516.   if (bl)
  517.     return BLOCKVECTOR_BLOCK (bl, index);
  518.   return 0;
  519. }
  520.  
  521. /* Return the function containing pc value PC.
  522.    Returns 0 if function is not known.  */
  523.  
  524. struct symbol *
  525. find_pc_function (pc)
  526.      CORE_ADDR pc;
  527. {
  528.   register struct block *b = block_for_pc (pc);
  529.   if (b == 0)
  530.     return 0;
  531.   return block_function (b);
  532. }
  533.  
  534. /* These variables are used to cache the most recent result
  535.  * of find_pc_partial_function. */
  536.  
  537. static CORE_ADDR cache_pc_function_low = 0;
  538. static CORE_ADDR cache_pc_function_high = 0;
  539. static char *cache_pc_function_name = 0;
  540.  
  541. /* Clear cache, e.g. when symbol table is discarded. */
  542.  
  543. void
  544. clear_pc_function_cache()
  545. {
  546.   cache_pc_function_low = 0;
  547.   cache_pc_function_high = 0;
  548.   cache_pc_function_name = (char *)0;
  549. }
  550.  
  551. /* Finds the "function" (text symbol) that is smaller than PC
  552.    but greatest of all of the potential text symbols.  Sets
  553.    *NAME and/or *ADDRESS conditionally if that pointer is non-zero.
  554.    Returns 0 if it couldn't find anything, 1 if it did.  On a zero
  555.    return, *NAME and *ADDRESS are always set to zero.  On a 1 return,
  556.    *NAME and *ADDRESS contain real information.  */
  557.  
  558. int
  559. find_pc_partial_function (pc, name, address)
  560.      CORE_ADDR pc;
  561.      char **name;
  562.      CORE_ADDR *address;
  563. {
  564.   struct partial_symtab *pst;
  565.   struct symbol *f;
  566.   struct minimal_symbol *msymbol;
  567.   struct partial_symbol *psb;
  568.  
  569.   if (pc >= cache_pc_function_low && pc < cache_pc_function_high)
  570.     {
  571.     if (address)
  572.         *address = cache_pc_function_low;
  573.     if (name)
  574.         *name = cache_pc_function_name;
  575.     return 1;
  576.     }
  577.  
  578.   pst = find_pc_psymtab (pc);
  579.   if (pst)
  580.     {
  581.       if (pst->readin)
  582.     {
  583.       /* The information we want has already been read in.
  584.          We can go to the already readin symbols and we'll get
  585.          the best possible answer.  */
  586.       f = find_pc_function (pc);
  587.       if (!f)
  588.         {
  589.         return_error:
  590.           /* No available symbol.  */
  591.           if (name != 0)
  592.         *name = 0;
  593.           if (address != 0)
  594.         *address = 0;
  595.           return 0;
  596.         }
  597.  
  598.       cache_pc_function_low = BLOCK_START (SYMBOL_BLOCK_VALUE (f));
  599.       cache_pc_function_high = BLOCK_END (SYMBOL_BLOCK_VALUE (f));
  600.       cache_pc_function_name = SYMBOL_NAME (f);
  601.       if (name)
  602.         *name = cache_pc_function_name;
  603.       if (address)
  604.         *address = cache_pc_function_low;
  605.       return 1;
  606.     }
  607.  
  608.       /* Get the information from a combination of the pst
  609.      (static symbols), and the minimal symbol table (extern
  610.      symbols).  */
  611.       msymbol = lookup_minimal_symbol_by_pc (pc);
  612.       psb = find_pc_psymbol (pst, pc);
  613.  
  614.       if (!psb && (msymbol == NULL))
  615.     {
  616.       goto return_error;
  617.     }
  618.       if (psb
  619.       && (msymbol == NULL
  620.           || (SYMBOL_VALUE_ADDRESS (psb)
  621.           >= msymbol -> address)))
  622.     {
  623.       /* This case isn't being cached currently. */
  624.       if (address)
  625.         *address = SYMBOL_VALUE_ADDRESS (psb);
  626.       if (name)
  627.         *name = SYMBOL_NAME (psb);
  628.       return 1;
  629.     }
  630.     }
  631.   else
  632.     /* Must be in the minimal symbol table.  */
  633.     {
  634.       msymbol = lookup_minimal_symbol_by_pc (pc);
  635.       if (msymbol == NULL)
  636.     goto return_error;
  637.     }
  638.  
  639.   {
  640.     if (msymbol -> type == mst_text)
  641.       cache_pc_function_low = msymbol -> address;
  642.     else
  643.       /* It is a transfer table for Sun shared libraries.  */
  644.       cache_pc_function_low = pc - FUNCTION_START_OFFSET;
  645.   }
  646.   cache_pc_function_name = msymbol -> name;
  647.   /* FIXME:  Deal with bumping into end of minimal symbols for a given
  648.      objfile, and what about testing for mst_text again? */
  649.   if ((msymbol + 1) -> name != NULL)
  650.     cache_pc_function_high = (msymbol + 1) -> address;
  651.   else
  652.     cache_pc_function_high = cache_pc_function_low + 1;
  653.   if (address)
  654.     *address = cache_pc_function_low;
  655.   if (name)
  656.     *name = cache_pc_function_name;
  657.   return 1;
  658. }
  659.  
  660. /* Return the innermost stack frame executing inside of the specified block,
  661.    or zero if there is no such frame.  */
  662.  
  663. #if 0    /* Currently unused */
  664.  
  665. FRAME
  666. block_innermost_frame (block)
  667.      struct block *block;
  668. {
  669.   struct frame_info *fi;
  670.   register FRAME frame;
  671.   register CORE_ADDR start = BLOCK_START (block);
  672.   register CORE_ADDR end = BLOCK_END (block);
  673.  
  674.   frame = 0;
  675.   while (1)
  676.     {
  677.       frame = get_prev_frame (frame);
  678.       if (frame == 0)
  679.     return 0;
  680.       fi = get_frame_info (frame);
  681.       if (fi->pc >= start && fi->pc < end)
  682.     return frame;
  683.     }
  684. }
  685.  
  686. #endif    /* 0 */
  687.  
  688. void
  689. _initialize_blockframe ()
  690. {
  691.   obstack_init (&frame_cache_obstack);
  692. }
  693. @
  694.